package com.kern.academic.bean;

import lombok.Data;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoshi at 8:04 2021-07-13
 */
@Data
public class SortInfo {

    public static final String SORT_DIRECTION_ASC = "asc";      // 升序
    public static final String SORT_DIRECTION_DESC = "desc";    // 降序


    public enum SortDirection {
        asc,
        desc
    }


    /**
     * 排序字段
     */
    private String property;

    /**
     * 排序顺序   升序或者降序
     */
    private SortDirection direction;


    public Sort toJpaSort() {
        List<String> properties = new ArrayList<>();
        properties.add(this.property);
        Sort.Direction direction = Sort.Direction.fromString(this.direction.name());
        return Sort.by(direction, properties.toArray(new String[0]));
    }



}
